package com.foreknow.dao.impl;

import com.foreknow.bean.Guestbook;
import com.foreknow.dao.GuestbookDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.util.OraclePagination;
import com.foreknow.util.Pagination;

import java.sql.SQLException;
import java.util.List;

public class GuestbookDaoImpl extends BaseDao implements GuestbookDao {
    @Override
    public boolean addMessage(Guestbook gb) throws SQLException {
        String sql = "insert into guestbook values(gb.nextval,?,?,?,?,?,?)";
        int isRigth = jdbcTemplate.update(sql,gb.getName(),gb.getPhone(),gb.getEmail(),gb.getTitle(),gb.getContent(),gb.getCreatedTime());
        return isRigth==1;
    }

    @Override
    public List<Object> getQueryAll() throws SQLException {
        String sql = "select * from guestbook";
        EntityMapping mapping = mappingFactory.mapping(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = jdbcTemplate.query(sql,mapping);
        return list;
    }

    @Override
    public boolean delById(Integer id) {
        String sql = "delete from guestbook where id=?";
        int isRigth = jdbcTemplate.update(sql,id);
        return isRigth==1;
    }

    @Override
    public Pagination getGuestbookByPage(int pageSize, int pageNumber) throws SQLException {
        String sql = "select * from guestbook";
        Pagination pagination = new OraclePagination(sql, pageSize, pageNumber);
        return pagination;
    }
}
